﻿
/*=====================================================================

  File:      FileStoreWriterStream.cs
  Summary:   SQL Remote Blob Storage sample provider. Support file for 
             a provider that uses NTFS files as the blob store.
             Write stream class override that includes a .Commit method.
  Date:	     June 24th, 2008

---------------------------------------------------------------------

  This file is part of the Microsoft SQL Server Code Samples.
  Copyright (C) Microsoft Corporation.  All rights reserved.

This source code is intended only as a supplement to Microsoft
Development Tools and/or on-line documentation.  See these other
materials for detailed information regarding Microsoft code samples.

THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

======================================================= */

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Security.AccessControl;

using Microsoft.Data.BlobStores;

namespace Microsoft.Data.BlobStores.FileBlobStore
{
    internal
    sealed
    class FileStoreWriterStream : BlobStoreWriterStream
    {
        public
        FileStoreWriterStream(string path, BlobInformation blobInformation)
        {
            m_writeStream = new FileStream(path, FileMode.Create, FileAccess.Write);
            m_blobInformation = blobInformation;
            m_Path = path;
        }


        /// <summary>
        /// This commits the blob to the backend store.
        /// The blob cannot be modified after this.
        /// Successful completion of this method indicates that the blob
        /// is durably stored on the backed blob store.
        /// Commit implicitly implies a Close(), 
        /// i.e. a Close() will not be called separately after Commit().
        /// </summary>
        /// <returns>
        /// Information about the blob just committed.
        /// </returns>
        public
        override
        BlobInformation Commit()
        {
            m_writeStream.Close();
            // Open outputPath again to retrieve blob size and length.
            //
            FileInfo fi = new FileInfo(m_Path);
            m_blobInformation.StoreBlobCreateTime = fi.CreationTimeUtc;
            m_blobInformation.BlobLength = fi.Length;
            return m_blobInformation;
        }

        public override bool CanRead
        {
            get { return m_writeStream.CanRead; }
        }

        public override bool CanSeek
        {
            get { return m_writeStream.CanSeek; }
        }

        public override bool CanWrite
        {
            get { return m_writeStream.CanWrite; }
        }

        public override long Length
        {
            get { return m_writeStream.Length; }
        }

        public override long Position
        {
            get { return m_writeStream.Position; }
            set { m_writeStream.Position = value; }
        }

        public override void Flush()
        {
            m_writeStream.Flush();
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            return m_writeStream.Seek(offset, origin);
        }

        public override void SetLength(long value)
        {
            m_writeStream.SetLength(value);
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            return m_writeStream.Read(buffer, offset, count);
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            m_writeStream.Write(buffer, offset, count);
        }


        private
        FileStream m_writeStream;

        private
        BlobInformation m_blobInformation;

        private
        string m_Path;
    }
}